define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            require(['jstree'], function () {
                var tid=0;
                var cateId;
                var type =2;
                // 初始化表格参数配置
                Table.api.init({
                    extend: {
                        index_url: 'yalimedia/media/index',
                        add_url: 'yalimedia/media/add?cateId='+tid+"&type="+type,
                        edit_url: 'yalimedia/media/edit?type='+type,
                        del_url: 'yalimedia/media/del?type='+type,
                        table: 'table',
                    }
                });

                var table = $("#table");

                // 初始化表格
                table.bootstrapTable({
                    url: $.fn.bootstrapTable.defaults.extend.index_url,
                    commonSearch:false,
                    pk: 'MediaId',
                    columns: [
                        [
                            {field: 'URL', title: __('图片'),visible:false,formatter: Table.api.formatter.image,events: Table.api.events.image},
                            {field: 'ImageTitle', title: __('标题'),visible:false},
                            {field: 'AudioTitle', title: __('音频')},
                            {field: 'VideoTitle', title: __('视频'),visible:false},
                            {field: 'Size', title: __('大小')},
                            {field: 'CoverURL', title: __('视频封面'),visible:false,formatter: Table.api.formatter.image},
                            {field: 'Status', title: __('状态'),formatter: Controller.api.formatter.status},
                            {field: 'CreationTime', title: __('上传时间')},
                            {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                        ]
                    ]
                });

                Table.api.bindevent(table);
                $(".nav-tabs a").click(function(){
                    type = $(this).attr('data-type');

                    switch($(this).attr('data-type')){
                        case "2":
                            $("#category_title").html('音频分组');
                            $("#btn_title").html('上传音频');
                            $('#table').bootstrapTable('hideColumn', "URL");
                            $('#table').bootstrapTable('hideColumn', "AudioTitle");
                            $('#table').bootstrapTable('hideColumn', "ImageTitle");
                            $('#table').bootstrapTable('hideColumn', "VideoTitle");
                            $('#table').bootstrapTable('hideColumn', "CoverURL");
                            $('#table').bootstrapTable('showColumn', "AudioTitle");
                            break;
                        case "3":
                            $("#category_title").html('视频分组');
                            $("#btn_title").html('上传视频');
                            $('#table').bootstrapTable('hideColumn', "URL");
                            $('#table').bootstrapTable('hideColumn', "AudioTitle");
                            $('#table').bootstrapTable('hideColumn', "ImageTitle");
                            $('#table').bootstrapTable('showColumn', "VideoTitle");
                            $('#table').bootstrapTable('showColumn', "CoverURL");
                            break;
                    }

                    $.fn.bootstrapTable.defaults.extend.add_url = 'yalimedia/media/add?cateId='+tid+"&type="+type;
                    $.fn.bootstrapTable.defaults.extend.edit_url = 'yalimedia/media/edit?type='+type;
                    $.fn.bootstrapTable.defaults.extend.del_url = 'yalimedia/media/del?type='+type;

                    table.bootstrapTable('refresh', {url:$.fn.bootstrapTable.defaults.extend.index_url+'?type='+$(this).attr('data-type')});
                    $('#channeltree').jstree(true).settings.core.data.data=function (node) {return {'id':node.id,'type':type};};
                    $('#channeltree').jstree(true).refresh();

                    $(".btn-category-edit,.btn-category-del").addClass("disabled");

                })


                $(".btn-category-add").on("click",function(){
                    var url = "yalimedia/category/add?type="+type;
                    Fast.api.open(url, __('Add'), {
                        callback:function () {
                            $('#channeltree').jstree(true).refresh(); // 刷新树
                        }
                    });
                })

                $(".btn-category-edit").on("click",function(){
                    var url = "yalimedia/category/edit?ids="+cateId;
                    Fast.api.open(url, __('编辑'), {
                        callback:function () {
                            $('#channeltree').jstree(true).refresh(); // 刷新树
                        }
                    });
                })


                $(".btn-category-del").on("click",function(){
                    Layer.confirm(__('Are you sure you want to delete this item?'), {icon: 3, title:'提示'}, function (index) {
                        var url = "yalimedia/category/del?ids="+cateId;
                        Fast.api.ajax({
                            url:url,
                        }, function () {
                            $('#channeltree').jstree(true).refresh(); // 刷新树
                            Layer.close(index);
                        });
                    });
                })

                $('#channeltree').on("changed.jstree", function (e, data) {
                    if(data.node){
                        tid = data.node.id;
                        var cateName  = data.node.text;

                        $.fn.bootstrapTable.defaults.extend.add_url = "yalimedia/media/add?cateId="+tid+"&cateName="+cateName+"&type="+type;
                        table.bootstrapTable('refresh', {url: $.fn.bootstrapTable.defaults.extend.index_url+"?cateid="+tid+"&type="+type,pageNumber:1});

                        if(data.node.type != 'total'){
                            cateId = data.node.id;
                            $(".btn-category-edit,.btn-category-del").removeClass("disabled");
                        }else{
                            $(".btn-category-edit,.btn-category-del").addClass("disabled");
                        }
                    }

                    return false;
                });

                $('#channeltree').jstree({
                    "checkbox": {"keep_selected_style": false,},
                    "types": {
                        "total": {"icon": "fa fa-folder-o",},
                        "folder": {"icon": "fa fa-folder",},
                        "disabled": {"check_node": false,"uncheck_node": false}
                    },
                    'plugins': ["types"],
                    "core": {
                        "themes": {
                            "stripes": false,
                            "dots": false,//是否显示树连接线
                            "icons": true,//是否显示节点的图标
                        },
                        'check_callback': true,
                        'data':{
                            'url' : 'yalimedia/media/tree',
                            'data' : function (node) {
                                return { 'id' : node.id,'type':type };
                            }
                        }
                    }
                });
            })
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            require(['jstree'], function () {
                var type = $("#c-type").val();
                $('#channeltree').jstree({
                    "checkbox": {"keep_selected_style": false,},
                    "types": {
                        "total": {"icon": "fa fa-folder-o",},
                        "folder": {"icon": "fa fa-folder",},
                        "disabled": {"check_node": false,"uncheck_node": false}
                    },
                    'plugins': ["types"],
                    "core": {
                        "themes": {
                            "stripes": false,
                            "dots": false,//是否显示树连接线
                            "icons": true,//是否显示节点的图标
                        },
                        'check_callback': true,
                        'data':{
                            'url' : 'yalimedia/media/upload_tree',
                            'data' : function (node) {
                                return { 'id' : node.id,'type':type};
                            }
                        }
                    }
                });

                $('#channeltree').on("changed.jstree", function (e, data) {
                    if(data.node){
                        $("#c-cateName").val(data.node.text);
                        $("#c-cateId").val(data.node.id);
                        $('#channeltree').hide()
                    }

                });

                $(document).click(function (event) {
                    var _con = $('#channeltree');  // 设置目标区域
                    if(!_con.is(event.target) && _con.has(event.target).length === 0){ // Mark 1
                        $('#channeltree').hide()
                    }
                });

                $("#c-cateName").click(function (event) {
                    event.stopPropagation();
                    $('#channeltree').show()
                })


            })


            Controller.api.bindevent();
        },
        api: {
            formatter:{
                status: function (value, row, index) {
                    switch (value) {
                        case "Normal":
                            return "正常";
                        case "Blocked":
                            return "屏蔽";
                        case "TranscodeFail":
                            return "转码失败";
                        case "Transcoding":
                            return "转码中";
                        case "UploadSucces":
                            return "上传完成";
                        case "UploadFail":
                            return "上传失败";
                        case "Uploading":
                            return "上传中";
                    }
                },
            },
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});